<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


defined('_JEXEC') or die();

class JobBoardModeljaTransactions extends JModel
{
    var $_data;
    var $_table;
    var $_pagination;
    var $_fieldListEdit;
    var $_table_name = 'ja_transactions';
    
    /**
    * Get field table instance
    * @return JTable Configuration table object
    */
    function &_getTable(){
        if($this->_table == null){
        	$this->_table = &JTable::getInstance('JA_Transactions', 'JobBoardTable');
        }
        return $this->_table;
	}

		/**
	* Get configuration item
	* @return JobboardTableJA_resumes Table object
	*/
	function getItem(){
		static $item;
		if (isset($item)) {
			return $item;
		}

        $table =& $this->_getTable(); 
        $table->_tbl_key ='id';
        
		// Load the current item if it has been defined
		$edit	= JRequest::getVar('edit',true);
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($cid, array(0));
		if ($edit) {
			$table->load($cid[0]);
			if ($table->id!=null)
				$table->cleanData();
		}
		$item = $table;
	    return $item;	    
	}
	
	
	function getItems()
	{
		$order = JRequest::getVar('filter_order');
		$order_dir = JRequest::getVar('filter_order_Dir');
		$filter_order = array();
		
		
		
		$order_clause = '';
		
		if ($order)
		{
			$filter_order['order'] = $order;
			$filter_order['order_dir'] = $order_dir;
			if ($order!='product_name')
				$order_clause = " ORDER BY $order $order_dir";		
		}
		else 
		{
			$order_clause = " ORDER BY a.created_date DESC";
			$filter_order['order'] = "a.created_date";
			$filter_order['order_dir'] = "DESC";
		}		
		$task = JRequest::getVar('task');
		$userid = JRequest::getVar('userid');
		$filter_status = JRequest::getVar('filter_status');
		$limit  = JRequest::getVar('limit');
        $limitstart            = JRequest::getVar('limitstart', 0, '' , 'int');
        if ($limit!=null)
	    	$_SESSION['subs_invoice_limit'] = $limit;
	    elseif(isset($_SESSION['subs_invoice_limit']))
	    	$limit = $_SESSION['subs_invoice_limit'];
	    else $limit = 20;
	    
		$db = JFactory::getDBO();
		$filter_credit_invoice = JRequest::getVar('filter_credit_invoice');
		$where = array();
		
		if (is_array($filter_credit_invoice))
			foreach ($filter_credit_invoice as $fieldname => $value)
				$where[] = '('.$fieldname.'='.$value.')';
		if (($filter_status!=null) && ($filter_status!=''))
			$where[] = '(a.completed="'.$filter_status.'")';
			
		//if ($task=='trans_viewbyuser')
		$where[] = 'a.user_id='.(int)$userid;
			
		$where_clause = implode(' AND ',$where);
		$query = 'SELECT a.*,b.name FROM #__ja_transactions AS a INNER JOIN #__users as b ON a.user_id=b.id ';
		if ($where_clause)
			$query.= ' WHERE '.$where_clause;
		if ($order_clause)
			$query.= $order_clause;
		jimport('joomla.html.pagination');

		$db->setQuery( $query);
		$items = $db->loadObjectList();
		$this->_pagination = new JPagination( count($items), $limitstart, $limit );	
		$paymentObject = get_payment_gateways();
		for ($i=0;$i<count($items);$i++)
		{
			for ($j=0;$j<count($paymentObject);$j++)
			{
				if ($items[$i]->payment_method==$paymentObject[$j]->id)
					$items[$i]->payment_name = $paymentObject[$j]->alias;

			}
			if (!isset($items[$i]->payment_name))
				$items[$i]->payment_name='';
			switch (strtolower($items[$i]->product_type))
			{
				case 'c':
					$items[$i]->product_name = $this->getProductNamefromID('ja_credits',$items[$i]->product_id);
					break;
				case 'p':
					$items[$i]->product_name = $this->getProductNamefromID('ja_posting_plans',$items[$i]->product_id);
					break;
				case 's':
					$items[$i]->product_name = $this->getProductNamefromID('ja_subscriptions',$items[$i]->product_id);
					break;
			}
			
			if ($items[$i]->completed)		$items[$i]->status_name = 'Completed';
			elseif($items[$i]->reason!='') 	$items[$i]->status_name = 'Reverse';			
			else $items[$i]->status_name = 'Pending';	
		}
		$list = array_slice( is_array($items) ? $items : array(), $this->_pagination->limitstart, $this->_pagination->limit );
			
		return $list;
	}	
	
	function getProductNamefromID($table_name, $product_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT title FROM #__$table_name WHERE id=".(int)$product_id;
		$db->setQuery($query);
		$id = $db->loadObject();
		if (isset($id->title))
			return $id->title;		
		else 
			return '';
	}	
	
	function getIDfromInvoice_id($table_name,$invoice_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT id FROM #__$table_name WHERE invoice_id=".(int)$invoice_id;
		$db->setQuery($query);
		$id = $db->loadObject();
		return $id->id;
	}
	
	function getPagination()
	{
		if (!$this->_pagination)
			$this->getItems();
		return $this->_pagination;
	}	
	
	function store($user_id = 0){
	    // Initialize variables
	    if ($user_id!=0)
	    {
	   		$user = & JFactory::getUser($user_id);
	   		JRequest::setVar('cid','');
	    }
	   	else 
	    	$user = & JFactory::getUser();
	    	
		$db		=& JFactory::getDBO();
		$row	=& $this->getItem();
		
		$post	= JRequest::getVar('arr_invoice_data');
		
		if ($post)
		foreach ($post as $k=>$value){
			if($k!='cid' && is_array($value)){
				$post[$k] = implode(',', $value);
			}
		}
		
		if (!$post)
			$post	= JRequest::get('request', JREQUEST_ALLOWHTML);
		
		
		
        	if (!$row->bind( $post )) 
        	{
			    return false;
		    }
		    
			if (!($temp=$row->store()))
		    {
		    	 			
		    	return false;
		    }
		return $row->id;
	}

	function remove()
	{
	    $cid = JRequest::getVar('cid',array(), 'post', 'array');
	    $n =  count($cid);
	    JArrayHelper::toInteger( $cid );
	    if($n)
	    {
            $query = "DELETE FROM #__ja_transactions WHERE id = ". implode(" OR id = ", $cid);
        	$db = &JFactory::getDBO();
            $db->setQuery($query);
            return $db->queryBatch();
        }
		return 1;
	} 

}
?>